/*需求:学生管理系统登录,注册,忘记密码的功能,登陆进去之后,可以进行增删改查功能
登陆界面:
--------------欢迎来到学生管理系统--------------
请选择操作:1.登录  2.注册  3.忘记密码
用户类:属性:用户名,密码,身份证号码,手机号码
注册功能:
1.用户名要满足以下要求:用户名唯一,长度在3-15位之间,只能是字母加数字的组合,但是不能纯数字
2.密码键盘输入两次,两次一致才可以进行注册
3.身份证号码要验证,长度为18位,不能以0开头,前17位,必须都是数字,最后一位可以是数字或X或x
4.手机号码要验证,长度为11位,不能以0开头,必须都是数字
登录功能:
1.键盘录入用户名
2.键盘录入密码
3.键盘录入验证码
要求:用户名未注册,直接结束方法,并提示:用户名未注册,请先注册;
     判断验证码是否正确,如不正确,重新输入
     再判断用户名和密码是否正确,有三次机会
验证码规则:4位大小写字母和1位数字随机组合
忘记密码:
1.键盘录入用户名,判断是否存在,如果不存在,提示:用户不存在
2.键盘录入身份证号码和手机号码
3.判断当前用户的身份证号码和手机号码是否一致,不一致则提示:账号信息不匹配,修改失败,如果一致,则提示输入密码,进行修改
*/
/*学生管理系统:

初始菜单:
--------------欢迎来到学生管理系统--------------
1:添加学生
2:删除学生
3:修改学生
4:查询学生
5:退出
请输入您的选择:

学生类:属性:id,姓名,年龄,家庭住址
添加功能:键盘录入每一个学生信息并添加,需要满足以下要求:id唯一
删除功能:键盘录入要删除学生的id,需要满足以下要求:1.id存在,删除成功并回到初始菜单 2.id不存在需要提示不存在并回到初始菜单
修改功能:键盘录入要修改学生的id,键盘录入新的学生信息,需要满足以下要求:1.id存在继续录入其他信息 2.id不存在需要提示不存在并回到初始菜单
查询功能:打印所有的学生信息,如果没有学生信息,提示:当前无学生信息,请添加后再查询
*/
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Random;
public class A42{
    public static void main(String[] args){
        ArrayList<User> userList = new ArrayList<>();
        while(true){
            System.out.println("--------------欢迎来到学生管理系统--------------");
            System.out.println("1.登录");
            System.out.println("2.注册");
            System.out.println("3.忘记密码");
            System.out.println("4.退出");
            System.out.println("请选择你要进行的操作");
            Scanner sc = new Scanner(System.in);
            int choose = sc.nextInt();
            switch(choose){
                case 1 -> login(userList);
                case 2 -> register(userList);
                case 3 -> forgetPassword(userList);
                case 4 -> {
                    System.out.println("4.退出");
                    System.exit(0);
                }
                default -> System.out.println("没有这个选项请重新输入");
            }
        }
    }
    public static void login(ArrayList<User> userList){

        /*要求:用户名未注册,直接结束方法,并提示:用户名未注册,请先注册;
        判断验证码是否正确,如不正确,重新输入
        再判断用户名和密码是否正确,有三次机会
        验证码规则:4位大小写字母和1位数字随机组合*/
        System.out.println("登录");
        Scanner sc = new Scanner(System.in);
        int count = 0;
        while (true) {
            System.out.println("请输入用户名");
            String username;
            username = sc.next();
            boolean flag1 = isExist(userList, username);
            if (!flag1) {
                System.out.println("用户名未注册,请先注册");
                return;
            }
            System.out.println("请输入密码");
            String password = sc.next();
            while (true) {
                    String rightCode = getCode();
                    System.out.println("验证码为:"+rightCode);
                    System.out.println("请输入验证码");
                    String code = sc.next();
                    if (code.equalsIgnoreCase(rightCode)) {
                        System.out.println("验证码正确");
                        break;
                    }else{
                        System.out.println("验证码错误,请重新输入");
                        continue;
                    }
            }
            //封装思想的应用:
            //我们可以把一些零散的数据,封装成一个对象
            //以后传递参数的时候,只要传递一个整体就可以了,不需要管这些零散的数据
            User user = new User(username,password,null,null);
            boolean flag2 = checkLogin(userList,user);
            if(flag2){
                System.out.println("登录成功");
                addStudentManagement();
                break;
            }else{
                count++;
                if(count<3){
                    System.out.println("用户名或密码错误,还有"+(3-count)+"次机会");
                }else{
                    System.out.println("登录失败,账号锁定");
                    break;
                }
            }
        }
    }
    public static void register(ArrayList<User> userList){
        System.out.println("注册");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名");
        String username;
        while (true) {
            username = sc.next();
            //在校验的时候先验证格式是否正确,再验证是否唯一
            //因为在以后所有的数据,都是存在数据库中,如果我们要校验,需要使用到网络资源.
            boolean flag1 =checkUsername(username);
            if(!flag1){
                System.out.println("用户名格式不正确,请重新输入");
                continue;
            }
            boolean flag2 = isExist(userList,username);
            if(flag2){
                System.out.println("用户名已存在,请重新输入");
                continue;
            }else{
                break;
            }
        }
        System.out.println("请输入密码");
        String password;
        while (true) {
            password = sc.next();
            System.out.println("请确认密码");
            String confirmPassword = sc.next();
            if(!confirmPassword.equals(password)){
                System.out.println("两次密码不一致,请重新输入");
                continue;
            }else{
                break;
            }
        }
        System.out.println("请输入身份证号码");
        String personId;
        while (true) {
            personId = sc.next();
            boolean flag3 = checkPersonId(personId);
            if(!flag3){
                System.out.println("身份证号码格式不正确,请重新输入");
                continue;
            }else{
                break;
            }
        }
        System.out.println("请输入手机号码");
        String phone;
        while (true) {
            phone = sc.next();
            boolean flag4 = checkPhone(phone);
            if(!flag4){
                System.out.println("手机号码格式不正确,请重新输入");
                continue;
            }else{
                break;
            }
        }
        User user = new User(username,password,personId,phone);
        userList.add(user);
        System.out.println("注册成功");
    }
    public static void forgetPassword(ArrayList<User> userList){
        System.out.println("忘记密码");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名");
        String username = sc.next();
        boolean flag1 = isExist(userList,username);
        if(!flag1){
            System.out.println("用户不存在");
            return;
        }
        System.out.println("请输入身份证号码");
        String personId = sc.next();
        System.out.println("请输入手机号码");
        String phone = sc.next();
        User user = new User(username,null,personId,phone);
        int flag2 = checkForgetPassword(userList,user);
        if(flag2<0){
            System.out.println("账号信息不匹配,修改失败");
        }else{
            System.out.println("请输入新密码");
            String newPassword = sc.next();
            userList.get(flag2).setPassword(newPassword);
            System.out.println("修改成功");
        }
    }
    public static int checkForgetPassword(ArrayList<User> userList,User user){
        for(int i = 0;i<userList.size();i++){
            if(userList.get(i).getUsername().equals(user.getUsername())&&userList.get(i).getIdCard().equals(user.getIdCard())&&userList.get(i).getPhone().equals(user.getPhone())){
                return i;
            }
        }
        return -1;
    }
    public static boolean checkLogin(ArrayList<User> userList,User user){
        for(int i = 0;i<userList.size();i++){
            if(userList.get(i).getUsername().equals(user.getUsername()) && userList.get(i).getPassword().equals(user.getPassword())){
                return true;
            }
        }
        return false;
    }
    public static String getCode(){
        StringBuilder sb = new StringBuilder();
        ArrayList<Character> list = new ArrayList<>();
        for(char i = 'a';i<='z';i++){
            list.add(i);
        }
        for(char i = 'A';i<='Z';i++){
            list.add(i);
        }
        Random sc = new Random();
        for(int i = 0;i<4;i++){
            sb.append(list.get(sc.nextInt(list.size())));
        }
        sb.append(sc.nextInt(10));
        char[] arr = sb.toString().toCharArray();
        int i = sc.nextInt(arr.length);
        char temp = arr[i];
        arr[i] = arr[arr.length-1];
        arr[arr.length-1] = temp;
        String code = new String(arr);
        return code;
    }
    public static boolean checkPhone(String phone){
        if(phone.length()!=11){
            return false;
        }
        if(phone.charAt(0)=='0'){
            return false;
        }
        for(int i = 0;i<phone.length();i++){
            char c = phone.charAt(i);
            if(!(c>='0'&&c<='9')){
                return false;
            }
        }
        return true;
    }
    public static boolean checkPersonId(String personId){
        if(personId.length() != 18){
            return false;
        }
        if(personId.charAt(0) == '0'){
            return false;
        }
        for(int i = 0; i < 17; i++){
            char c = personId.charAt(i);
            if(!(c>='0'&& c<='9')){
                return false;
            }
        }
        char lastChar = personId.charAt(17);
        if(!((lastChar>='0'&& lastChar<='9') || lastChar == 'X' || lastChar == 'x')){
            return false;
        }
        return true;
    }
    public static boolean isExist(ArrayList<User> userList,String username){
        int index = getIndex(userList,username);
        if(index<0){
            return false;
        }else{
            return true;
        }
    }
    public static int getIndex(ArrayList<User> userList,String username){
        for(int i = 0; i < userList.size(); i++){
            if(username.equals(userList.get(i).getUsername())){
                return i;
            }
        }
        return -1;
    }
    public static boolean checkUsername(String username){
        int len = username.length();
        if(len < 3 || len > 15){
            return false;
        }
        for(int i = 0; i < len; i++){
            char c = username.charAt(i);
            if(!((c>='a'&& c<='z') || (c>='A'&& c<='Z') || (c>='0'&& c<='9'))){
                return false;
            }
        }
        for(int i = 0; i < len; i++){
            char c = username.charAt(i);
            if((c>='a'&& c<='z') || (c>='A'&& c<='Z')){
                return true;
            }
        }
        return false;
    }
    public static class User{
        private String username;
        private String password;
        private String idCard;
        private String phone;
        public User() {
        }
        public User(String username, String password, String idCard, String phone) {
            this.username = username;
            this.password = password;
            this.idCard = idCard;
            this.phone = phone;
        }
        public String getUsername() {
            return username;
        }
        public void setUsername(String username) {
            this.username = username;
        }
        public String getPassword() {
            return password;
        }
        public void setPassword(String password) {
            this.password = password;
        }
        public String getIdCard() {
            return idCard;
        }
        public void setIdCard(String idCard) {
            this.idCard = idCard;
        }
        public String getPhone() {
            return phone;
        }
        public void setPhone(String phone) {
            this.phone = phone;
        }
    }
    public static void addStudentManagement() {
        ArrayList<Student> stuList = new ArrayList<>();
        loop:
        while (true) {
            System.out.println("--------------欢迎来到学生管理系统--------------");
            System.out.println("1:添加学生");
            System.out.println("2:删除学生");
            System.out.println("3:修改学生");
            System.out.println("4:查询学生");
            System.out.println("5:退出");
            System.out.println("请输入您的选择:");
            Scanner sc = new Scanner(System.in);
            int choice = sc.nextInt();
            switch (choice) {
                case 1 -> addStudent(stuList);
                case 2 -> deleteStudent(stuList);
                case 3 -> updateStudent(stuList);
                case 4 -> queryStudent(stuList);
                case 5 -> {
                    System.out.println("退出");
                    break loop;
                    //System.exit(0);用这个也行
                }
                default -> System.out.println("没有这个选项,请重新输入");
            }
        }
    }
    public static void addStudent(ArrayList<Student> stuList){

        System.out.println("添加学生");
        Scanner sc = new Scanner(System.in);
        String id = null;
        while (true) {
            System.out.print("请输入id:");
            id = sc.next();
            if(isExist1(stuList,id)){
                System.out.println("id已存在,请重新输入");
                continue;
            }else{
                break;
            }
        }
        System.out.print("请输入姓名:");
        String name = sc.next();
        System.out.print("请输入年龄:");
        int age = sc.nextInt();
        System.out.print("请输入家庭住址:");
        String address = sc.next();
        Student stu = new Student(id, name, age, address);
        stuList.add(stu);
        System.out.println("添加成功");
    }
    public static void deleteStudent(ArrayList<Student> stuList){
        System.out.println("删除学生");
        System.out.println("请输入要删除学生的id");
        Scanner sc = new Scanner(System.in);
        String id = sc.next();
        int index = getIndex1(stuList,id);
        if(index<0){
            System.out.println("id不存在");
        }else{
            stuList.remove(index);
            System.out.println("删除成功");
        }
    }
    public static void updateStudent(ArrayList<Student> stuList){
        System.out.println("修改学生");
        System.out.println("请输入要修改学生的id");
        Scanner sc = new Scanner(System.in);
        String id = sc.next();
        int index = getIndex1(stuList,id);
        if(index<0){
            System.out.println("id不存在");
        }
        if(index>=0){
            Student stu = new Student();
            System.out.print("请输入新的id:");
            stu.setId(sc.next());
            System.out.print("请输入新的姓名:");
            stu.setName(sc.next());
            System.out.print("请输入新的年龄:");
            stu.setAge(sc.nextInt());
            System.out.print("请输入新的家庭住址:");
            stu.setAddress(sc.next());
            stuList.set(index,stu);
            System.out.println("修改成功");
        }
    }
    public static void queryStudent(ArrayList<Student> stuList){
        System.out.println("查询学生");
        if(stuList.size() == 0){
            System.out.println("当前无学生信息,请添加后再查询");
            return;
        }
        System.out.println("id\t\t姓名\t年龄\t家庭住址");
        for(int i = 0;i<stuList.size();i++){
            System.out.println(stuList.get(i).getId()+"\t\t"+stuList.get(i).getName()+"\t"+stuList.get(i).getAge()+"\t"+stuList.get(i).getAddress());
        }
    }
    public static boolean isExist1(ArrayList<Student> stuList, String id){
        if(getIndex1(stuList,id)<0){
            return false;
        }else{
            return true;
        }
    }
    public static int getIndex1(ArrayList<Student> stuList, String id){
        for(int i = 0;i<stuList.size();i++){
            if(stuList.get(i).getId().equals(id)){
                return i;
            }
        }
        return -1;
    }

    public static class Student{
        private String id;
        private String name;
        private int age;
        private String address;
        public Student(){
        }
        public Student(String id, String name, int age, String address){
            this.id = id;
            this.name = name;
            this.age = age;
            this.address = address;
        }
        public String getId() {
            return id;
        }
        public void setId(String id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
        public String getAddress() {
            return address;
        }
        public void setAddress(String address) {
            this.address = address;
        }
    }



}
